/**
 * 实体
 */
Ext.define('cc.self.view.SelfBus', {
	extend: 'Ext.form.Panel',
	uses: ['cc.self.util.SelfService'],
	layout: {
		type: 'vbox',
		align: 'stretch'
	},
	autoScroll:true,
	//是否显示编辑器
	enableEdt: true,
	
	viewModel: {
		data: {
			rec: Ext.create('cc.self.model.SelfBus')
		},
		formulas: {
			paying: function (get){
				return get('rec.jh_money') - get('rec.payed_money');
			},
			other: function (get) {
				return get('rec.ny_money') + get('rec.zf_money') 
				+ get('rec.dz_money') + get('rec.kj_money') + get('rec.dl_money');
			},
			profit: function (get) {
				return get('rec.jh_money') + get('rec.ts_money')
				- get('rec.gc_money') - get('other');
			},
			ywy: function (get) {
				return Ext.Number.toFixed(get('profit')*0.1, 2);
			},
			isDraft: function(get) {
				return get('rec.status') == 0 ? true: false;
			},
			isAvailable: function(get) {
				return get('rec.status') == 1 ? true: false;
			}
		}
	},
	defaults: {
		xtype: 'fieldset',
		border: 3,
		style: {
		    borderColor: 'gray',
		    borderStyle: 'solid'
		},
		margin: '10 10 10 10',
		layout: {
			type: 'vbox',
			align: 'stretch'
		},
		defaults: {
			xtype: 'container',
			margin: '5 5 10 5',
			layout: 'hbox',
			defaults: {
				labelAlign:'right',
				validateOnChange: false,
				flex: 1
			}
		}
	},
	initComponent: function() {
		var me = this;
		me.tbar = [{
			text: '转为可结算状态',
			bind: {
				disabled: '{!isDraft}'
			},
			handler: function(){
				CC.Self.updateSelfBusStatus({
					id: me.getViewModel().get('rec').get('id'),
					status: 1
				},function(){
					var rec = me.getViewModel().get('rec');
					rec.set('status', 1); rec.commit();
					me.up('window').close();
				},function(msg){
					Ext.Msg.alert('失败', msg);
				});
			}
		},{
			text: '结算完毕',
			bind: {
				disabled: '{!isAvailable}'
			},
			handler: function() {
				CC.Self.updateSelfBusStatus({
					id: me.getViewModel().get('rec').get('id'),
					status: 2
				},function(){
					var rec = me.getViewModel().get('rec');
					rec.set('status', 2); rec.commit();
					me.up('window').close();
				},function(msg){
					Ext.Msg.alert('失败', msg);
				});
			}
		}];
		me.items = [{
			title: '基本信息',
			items: [{
				items: [{
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '编号',
							bind: '{rec.code}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '外销发票号',
							bind: '{rec.wx_no}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '公司发票号',
							bind: '{rec.gs_no}'
				}]
			},{
				items: [{
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '客户名称',
							bind: '{rec.customer}'
						}, {
							xtype: me.enableEdt ? 'datefield' : 'displayfield',
							format: 'Y-m-d',
							fieldLabel: '报关日期',
							bind: '{rec.bg_date}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '报关单号',
							bind: '{rec.bg_no}'
				}]
			},{
				items: [{
							xtype: me.enableEdt ? 'numberfield' : 'displayfield',
							fieldLabel: '报关金额',
							bind: '{rec.bg_money}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '启运港',
							bind: '{rec.port_from}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '目的港',
							bind: '{rec.port_to}'
				}]
			},{
				items: [{
							xtype: me.enableEdt ? 'datefield' : 'displayfield',
							format: 'Y-m-d',
							fieldLabel: '退税申报日期',
							bind: '{rec.ts_date}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '贸易方式',
							bind: '{rec.bus_way}'
						}, {
							xtype: me.enableEdt ? 'textfield' : 'displayfield',
							fieldLabel: '收汇方式',
							bind: '{rec.get_way}'
				}]
			}]
		},{
			xclass: 'cc.self.view.SelfJhGridView',
			border: false,
			viewModel: {
				data: {
					rec: me.getViewModel().get('rec')
				}
			},
			updateJh: function(v){
				me.getViewModel().get('rec').set('jh_money', v);
			}
		}, {
			title: '其他费用',
			items: [{
				items: [{
					xtype: me.enableEdt ? 'textfield' : 'displayfield',
					fieldLabel: '内运费开票单位',
					bind: '{rec.ny_org}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '内运费发票日期',
					bind: '{rec.ny_date}'
				}, {
					xtype: me.enableEdt ? 'numberfield' : 'displayfield',
					fieldLabel: '内运费',
					bind: '{rec.ny_money}'
				}]
			},{
				items: [{
					xtype: me.enableEdt ? 'textfield' : 'displayfield',
					fieldLabel: '杂费开票单位',
					bind: '{rec.zf_org}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '杂费发票日期',
					bind: '{rec.zf_date}'
				}, {
					xtype: me.enableEdt ? 'numberfield' : 'displayfield',
					fieldLabel: '相关杂费',
					bind: '{rec.zf_money}'
				}]
			},{
				items: [{
					xtype: me.enableEdt ? 'textfield' : 'displayfield',
					fieldLabel: '单证费开票单位',
					bind: '{rec.dz_org}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '单证费发票日期',
					bind: '{rec.dz_date}'
				}, {
					xtype: me.enableEdt ? 'numberfield' : 'displayfield',
					fieldLabel: '单证费',
					bind: '{rec.dz_money}'
				}]
			},{
				items: [{
					xtype: me.enableEdt ? 'textfield' : 'displayfield',
					fieldLabel: '快件费开票单位',
					bind: '{rec.kj_org}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '快件费发票日期',
					bind: '{rec.kj_date}'
				}, {
					xtype: me.enableEdt ? 'numberfield' : 'displayfield',
					fieldLabel: '单证费',
					bind: '{rec.kj_money}'
				}]
			},{
				items: [{
					xtype: me.enableEdt ? 'textfield' : 'displayfield',
					fieldLabel: '代理费开票单位',
					bind: '{rec.dl_org}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '代理费发票日期',
					bind: '{rec.dl_date}'
				}, {
					xtype: me.enableEdt ? 'numberfield' : 'displayfield',
					fieldLabel: '代理费',
					bind: '{rec.dl_money}'
				}]				
			}]
		},{
			xclass: 'cc.self.view.SelfFactoryGridView',
			border: false,
			viewModel: {
				data: {
					rec: me.getViewModel().get('rec')
				}
			},
			updateGcAndPayed: function(gc, payed){
				var rec = me.getViewModel().get('rec');
				rec.set('gc_money', gc);
				rec.set('payed_money', payed);
			}
		},{
			title: '统计',
			items: [{
					items: [{
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '结汇收入',
						bind: '{rec.jh_money}'
					}, {
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '工厂货款',
						bind: '{rec.gc_money}'
					},{
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '退税收入',
						bind: '{rec.ts_money}'
					}, {
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '其他费用',
						bind: '{other}'
					}]
				},{
					items: [{
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '已付货款',
						bind: '{rec.payed_money}'
					}, {
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '可付货款',
						bind: '{paying}'
					},{
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '业务利润',
						bind: '{profit}'
					}, {
						xtype: me.enableEdt ? 'numberfield' : 'displayfield',
						readOnly: true,
						fieldCls: 'cc_readonly',
						fieldLabel: '业务员提成',
						bind: '{ywy}'
					}]
				}
			 
			]
		}, {
			title: '相关人员',
			items: [{
				items: [{
						xtype: me.enableEdt ? 'textfield' : 'displayfield',
						fieldLabel: '业务员',
						bind: '{rec.ywy}'
					},{
						xtype: me.enableEdt ? 'textfield' : 'displayfield',
						fieldLabel: '制单人',
						bind: '{rec.zdr}'
					},{
						xtype: me.enableEdt ? 'textfield' : 'displayfield',
						fieldLabel: '审核人',
						bind: '{rec.shr}'
					},{
						xtype: me.enableEdt ? 'textfield' : 'displayfield',
						fieldLabel: '存档人',
						bind: '{rec.cdr}'
					} ]
			},{
				items: [{
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					format: 'Y-m-d',
					fieldLabel: '业务员日期',
					bind: '{rec.ywy_date}'
				}, {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					fieldLabel: '制单日期',
					format: 'Y-m-d',
					bind: '{rec.zd_date}'
				},  {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					fieldLabel: '审核日期',
					format: 'Y-m-d',
					bind: '{rec.sh_date}'
				},  {
					xtype: me.enableEdt ? 'datefield' : 'displayfield',
					fieldLabel: '存档日期',
					format: 'Y-m-d',
					bind: '{rec.cd_date}'
				}]
			}]
		},{
			title: '备注',
			items: [{
				items: {
					xtype: me.enableEdt ? 'textarea' : 'displayfield',
					allowBlank: true,
					emptyText: '备注',
					bind: '{rec.remark}'
				}
			}]
		}];
		me.callParent(arguments);
	},

	save: function(cb) {
		var me = this;
		var rec = me.getViewModel().getData().rec;
		var d = rec.getData();
		if(rec.get('id') > 0){
			//update
			CC.Self.updateSelfBus({data: Ext.encode(d)}, function(){
				rec.commit();
				if(cb){
					cb(rec);
				}
			});
		}
	},
	
	loadEntity: function(key){
		var me = this;
		CC.Self.getSelfBus(key, function(data){
			me.getViewModel().set('rec', Ext.create('cc.self.model.SelfBus', data));
		});
	}
});
